import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-04-05
 * Time: 12:43
 */
public class BinaryTree {

    static class TreeNode {
        public Object val;//值
        public TreeNode left;//左孩子
        public TreeNode right;//右孩子

        public TreeNode(String val) {
            this.val = val;
        }
    }

    public TreeNode root;//根节点

    //枚举创建二叉树
    public TreeNode createTree() {
        TreeNode A = new TreeNode("A");
        TreeNode B = new TreeNode("B");
        TreeNode C = new TreeNode("C");
        TreeNode D = new TreeNode("D");
        TreeNode E = new TreeNode("E");
        TreeNode F = new TreeNode("F");
        TreeNode G = new TreeNode("G");
        TreeNode H = new TreeNode("H");
        TreeNode I = new TreeNode("I");
        A.left = C;
        C.left = D;
        D.left = G;
        D.right = H;
        A.right = B;
        B.left = E;
        B.right = F;
        E.left = I;
        return A;
    }

    //判断第k层的结点个数
    public int KLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return KLevelNodeCount(root.left, k - 1)
                + KLevelNodeCount(root.right, k - 1);
    }

    //树的结点个数
    public int NodeCount(TreeNode root) {

        if (root == null) {
            return 0;
        }

        return NodeCount(root.left) + NodeCount(root.right) + 1;
    }

    //叶子结点个数
    public int leafCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return leafCount(root.right) + leafCount(root.left);
    }

    //树的最大高度
    public int getHeight(TreeNode root) {

        if (root == null) {
            return 0;
        }

        //max(树的左子树高度,右子树高度)+1
        return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
    }

    //判断val是否存在树中
    public boolean find(TreeNode root, String val) {
        if (root == null) {
            return false;
        }
        if (root.val.equals(val)) {
            return true;
        }
        return find(root.left, val) || find(root.right, val);
    }

}